package com.shenyong.serviceImpl;

import com.shenyong.controller.BaseController;
import com.shenyong.entity.Collection;
import com.shenyong.entity.LoginUsers;
import com.shenyong.entity.Users;
import com.shenyong.entity.Users_Concern;
import com.shenyong.exception.MyException;
import com.shenyong.mapper.UserMapper;
import com.shenyong.service.UserService;
import com.shenyong.util.JwtUtil;
import com.shenyong.util.MD5Untils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.LockedAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author
 * @site
 * @company
 * @create
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private BaseController baseController;
    @Autowired
    private UserMapper userMapper;

    public LoginUsers getName(String username) {
        Users user1 = null;

        user1 = userMapper.getUserByName(username);

        if (user1 == null) {
            throw new MyException("该用户名不存在,请检查后再登录!(๑¯◡¯๑)");
        }
       /* if (!MD5Untils.verify(user1.getUser_password(),user1.getUser_password())) {
            throw new AccountException("密码错误，请仔细一点哦(`･ω･′)ゞ");
        }*/
        if (user1.getUser_enabled() == 1){
            throw new LockedAccountException("用户被锁定,请联系管理员!๐·°(৹˃̵﹏˂̵৹)°·๐");
        }
        //Map<String,Object> map = new HashMap<>();
       // List<String> rolePre = userMapper.selectRoleDetails(user1.getUser_id());
        //map.put("prems",rolePre);
        LoginUsers loginUser=new LoginUsers();
        //根据电话号码和密码加密生成token
        String token = JwtUtil.sign(user1.getUser_username(), user1.getUser_password());
        loginUser.setToken(token);
        //loginUser.setPrems(rolePre);
        loginUser.setUser(user1);
        return loginUser;
    }

    @Override
    public LoginUsers getUsers(Users users) {
        Users user1 = null;
        user1=userMapper.getUserByName(users.getUser_username());
        if (user1 == null) {
            throw new MyException("该用户名不存在,请检查后再登录!(๑¯◡¯๑)");
        }
        if (!MD5Untils.verify(users.getUser_password(),user1.getUser_password())) {
            throw new AccountException("密码错误，请仔细一点哦(`･ω･′)ゞ");
        }
        if (user1.getUser_enabled() == 1){
            throw new LockedAccountException("用户被锁定,请联系管理员!๐·°(৹˃̵﹏˂̵৹)°·๐");
        }
        //Map<String,Object> map = new HashMap<>();
        // List<String> rolePre = userMapper.selectRoleDetails(user1.getUser_id());
        //map.put("prems",rolePre);
        LoginUsers loginUser=new LoginUsers();
        //根据电话号码和密码加密生成token
        String token = JwtUtil.sign(user1.getUser_username(), user1.getUser_password());
        loginUser.setToken(token);
        //loginUser.setPrems(rolePre);
        loginUser.setUser(user1);
        return loginUser;
    }

    @Override
    public int updatePasword(Users users) {
        return userMapper.updatePasword(users);
    }
    @Override
    public int Allguanzhu(Users_Concern users_Concern) {
        System.out.println(users_Concern+"DASDSADSADAS");
        List<Users_Concern> aa=userMapper.AllUsers_Concern(users_Concern);
        return aa.size();
    }

    @Override
    public int upguanzhu(Users_Concern users_Concern) {
        List<Users_Concern> aa=userMapper.AllUsers_Concern(users_Concern);
        if (aa.size()==1){
            userMapper.delUsers_Concern(users_Concern);
            return 0;
        }else{
            userMapper.addUsers_Concern(users_Concern);
            return 1;
        }
    }

    @Override
    public int Allshoucang(Collection collection) {
        List<Collection> aa=userMapper.AllCollection(collection);
        return aa.size();
    }

    @Override
    public int upshoucang(Collection collection) {
        List<Collection> aa=userMapper.AllCollection(collection);
        if (aa.size()==1){
            userMapper.delCollection(collection);
            return 0;
        }else{
            userMapper.addCollection(collection);
            return 1;
        }
    }
}
